package cn.iotab.iotkit.server.demo.device.handler;

import cn.iotab.iotkit.transport.api.access.device.listener.DeviceEventHandler;
import cn.iotab.iotkit.transport.api.access.model.custom.event.EventMessage;
import cn.iotab.iotkit.transport.api.access.model.message.CommonDeviceMessageReply;
import cn.iotab.iotkit.transport.api.access.model.message.DeviceMessageReply;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 设备向平台发送event，可用于设备向平台上报事件
 * 请求示例：
 * <pre>
 * {
 *   "id": "123",
 *   "ver": "1.0",
 *   "ack":1,
 *   "type": "event.myEventA",
 *   "param": {
 *     "aa": "bb"
 *   }
 *  }
 *  </pre>
 * 响应示例:
 * {"code":200,"id":"123"}
 */
@Slf4j
@Component
public class MyEventAHandler extends DeviceEventHandler {
    @Override
    protected ListenableFuture<DeviceMessageReply> doProcess(EventMessage message) {
        log.info("receive eventId: {}, detail:{}", message.getEventId(), message.toOriginMessage());
        //
        CommonDeviceMessageReply reply = new CommonDeviceMessageReply();
        reply.from(message);
        reply.setSuccess();
        return Futures.immediateFuture(reply);
    }
}
